package com.eightywork.life.activity;

import java.util.List;

import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import cn.jpush.android.api.JPushInterface;

import com.eightywork.life.R;
import com.eightywork.life.adapter.MyCouponListAdapter;
import com.eightywork.life.adapter.MyEventListAdapter;
import com.eightywork.life.adapter.MyServiceListAdapter;
import com.eightywork.life.model.Coupon;
import com.eightywork.life.model.Event;
import com.eightywork.life.model.MyService;
import com.eightywork.life.service.SpaceService;
import com.eightywork.life.util.MyHttpException;
import com.eightywork.life.widget.FooterView;
import com.eightywork.life.widget.MyListView;
import com.eightywork.life.widget.MyListView.OnRefreshListener;
import com.umeng.analytics.MobclickAgent;

public class MySECListActivity extends BaseActivity implements OnClickListener {

	private ImageButton backButton;
	private MyServiceListAdapter msAdapter;
	private MyEventListAdapter  meAdapter;
	private MyCouponListAdapter mcAdapter;
	private TextView headerTitile,footerText, tips; 
	private LinearLayout ll_noMsg, ll_listView;
	private MyListView listView;
	private boolean isLoading, isRefresh = false,isFromEvent, isFromService, isFromCoupon;
	private int page = 1;
	private FooterView footerView;
	private int flag = 0; //第一次加载
	private AsyncTask<?, ?, ?> sTask, eTask, cTask;
//	private ViewGroup footView, nodataView;
	private List<MyService> sList;
	private List<Event>   eList;
	private List<Coupon>   cList;
//	private long count;
	@Override
	protected void onCreate(Bundle arg0) {
		// TODO Auto-generated method stub
		super.onCreate(arg0);
		setContentView(R.layout.activity_notice_list);	
		initView();
		initData();
		if (isFromService) {
			getMyServiceList();
		} else if (isFromEvent) {
			getMyEventList();
		} else if (isFromCoupon) {
			getMyCouponList();
		}
		
	}
	@Override
	protected void onDestroy() {
		if (sTask != null) {
			sTask.cancel(true);
		}
		if (eTask != null) {
			eTask.cancel(true);
		}
		if (cTask != null) {
			cTask.cancel(true);
		}
		super.onDestroy();
	}
	
	@Override
	public void onResume() {		
		super.onResume();
		JPushInterface.onResume(this);
	    MobclickAgent.onResume(this);
	}
	
	@Override
	public void onPause() {
	    super.onPause();
		JPushInterface.onPause(this);
	    MobclickAgent.onPause(this);
	}

	
	private void initView() {
		backButton = (ImageButton)findViewById(R.id.button_back);
		headerTitile = (TextView)findViewById(R.id.header_title);
		listView = (MyListView)findViewById(R.id.listView2);
		ll_noMsg = (LinearLayout) findViewById(R.id.noMsg);
		ll_listView = (LinearLayout) findViewById(R.id.ll_listView);
		tips = (TextView) findViewById(R.id.tips);
		footerView = new FooterView(this);
		footerView.hideAll();
		backButton.setOnClickListener(this);
		
	}
	
	private void initData() {
		isFromEvent = getIntent().getBooleanExtra("isFromEvent", false);
		isFromService = getIntent().getBooleanExtra("isFromService", false);
		isFromCoupon = getIntent().getBooleanExtra("isFromCoupon", false);
		if(isFromService) {
			initHeader("我预约的服务");
			msAdapter = new MyServiceListAdapter(context, null);
			listView.setAdapter(msAdapter);
		} else if (isFromEvent) {
			initHeader("我参与的活动");
			meAdapter = new MyEventListAdapter(context, null);
			listView.setAdapter(meAdapter);
		} else if (isFromCoupon) {
			initHeader("我的优惠券");
			mcAdapter = new MyCouponListAdapter(context, null);
			listView.setAdapter(mcAdapter);
		}
		listView.setonRefreshListener(new OnRefreshListener() {
			
			@Override
			public void onRefresh() {
				// TODO Auto-generated method stub		
				page = (flag == 0)? 1: 0;
				isLoading = false;
				isRefresh = true;
				if (isFromService) {
					sTask = new MyServiceListTask().execute();
				} else if (isFromEvent) {
					eTask = new MyEventListTask().execute();
				} else if (isFromCoupon) {
					cTask = new MyCouponListTask().execute();
				}
				
			}
		});
//		listView.setOnScrollStopAndEndListener(new OnScrollStopAndEndListener() {
//			
//			@Override
//			public void onEndAndStopScroll() {
//				// TODO Auto-generated method stub
//				if (!isLoading) {
//					getPmList();
//				}
//			}
//		});
//		listView.goLoading();
		
	}
	
	private void getMyServiceList() {
		sTask = new MyServiceListTask().execute();
		
	}
	
	private void getMyEventList() {
		eTask = new MyEventListTask().execute();
	}
	
    private void getMyCouponList() {
		cTask = new MyCouponListTask().execute();
	}
	
	//我预约的服务
	class MyServiceListTask extends AsyncTask<Void, Void, String> {
		List<MyService> mslist;
		@Override
		protected void onPreExecute() {
			if(flag == 0){
				showProgressDialog("正在加载中...");
			}else {
				page = page + 1;
			}
			isLoading = true;
			super.onPreExecute();
		}

		@Override
		protected String doInBackground(Void... params) {
			// TODO Auto-generated method stub
			try {
				mslist = SpaceService.getMySerice(page + "", "");
				
			} catch (MyHttpException e) {
				// TODO Auto-generated catch block
				showMessage(e.getErrorMessage());
			}
			return null;
		}
		
		@Override
		protected void onPostExecute(String result) {
			super.onPostExecute(result);
			isLoading = false;
			if (flag == 0) {
				hideProgressDialog(); 
			}
			if (isRefresh) {	
				listView.onRefreshComplete();
			} 
			if (mslist != null && mslist.size() > 0) {
				if (flag == 0) {
					msAdapter = new MyServiceListAdapter(context, mslist);
					listView.addFooterView(footerView.getFooter());
					listView.setAdapter(msAdapter);
			    	listView.setOnScrollListener(new ScrollListener());
			    	sList = mslist;
					
				} else if (isRefresh) {
					isRefresh = false;
					msAdapter = new MyServiceListAdapter(context, mslist);				
					listView.setAdapter(msAdapter);
					listView.setOnScrollListener(new ScrollListener());
					sList = mslist;
					listView.onRefreshComplete();
				} else {
					sList.addAll(mslist);
					msAdapter.notifyDataSetChanged();
				}
				if (mslist.size() < 5) {
					footerView.showNoData();
				}
			} else {
				if (mslist != null) {
					if (flag == 0) {
						ll_listView.setVisibility(View.GONE);
						ll_noMsg.setVisibility(View.VISIBLE);
						tips.setText("暂时还没预约服务");
					} else {
						footerView.showNoData();
					}			
				} else {
					footerView.hideAll();
					showMessage("网络链接失败，请稍后重试！");
				}
				
			}
			flag = 1;
			
		}
		
	}
	
	//我的优惠券
	
	class MyCouponListTask extends AsyncTask<Void, Void, String> {
		List<Coupon> mclist;
		@Override
		protected void onPreExecute() {
			if(flag == 0){
				showProgressDialog("正在加载中...");
			}else {
				page = page + 1;
			}
			isLoading = true;
			super.onPreExecute();
		}

		@Override
		protected String doInBackground(Void... params) {
			// TODO Auto-generated method stub
			try {
				mclist = SpaceService.getMyCoupon(page + "", "");
				
			} catch (MyHttpException e) {
				// TODO Auto-generated catch block
				showMessage(e.getErrorMessage());
			}
			return null;
		}
		
		@Override
		protected void onPostExecute(String result) {
			super.onPostExecute(result);
			isLoading = false;
			if (flag == 0) {
				hideProgressDialog(); 
			}
			if (isRefresh) {	
				listView.onRefreshComplete();
			} 
			if (mclist != null && mclist.size() > 0) {			
				if (flag == 0) {
					mcAdapter = new MyCouponListAdapter(context, mclist);
					listView.addFooterView(footerView.getFooter());
					listView.setAdapter(mcAdapter);
			    	listView.setOnScrollListener(new ScrollListener());
			    	cList = mclist;
					
				} else if (isRefresh) {
					isRefresh = false;
					mcAdapter = new MyCouponListAdapter(context, mclist);		
					listView.setAdapter(mcAdapter);
					listView.setOnScrollListener(new ScrollListener());
					cList = mclist;
					listView.onRefreshComplete();
				} else {
					cList.addAll(mclist);
					mcAdapter.notifyDataSetChanged();
				}
				if (mclist.size() < 5) {
					footerView.showNoData();
				}
			} else {
				
				if (mclist != null) {
					if (flag == 0) {
						ll_listView.setVisibility(View.GONE);
						ll_noMsg.setVisibility(View.VISIBLE);
						tips.setText("暂时还没优惠券");
					} else {
						footerView.showNoData();
					}		
				} else {				
					footerView.hideAll();
					showMessage("网络链接失败，请稍后重试！");
				}
				
			}
			flag = 1;
			
		}
		
	}
	
	//我参与的活动
	class MyEventListTask extends AsyncTask<Void, Void, String> {
		List<Event> melist;
		@Override
		protected void onPreExecute() {
			if(flag == 0){
				showProgressDialog("正在加载中...");
			}else {
				page = page + 1;
			}
			isLoading = true;
			super.onPreExecute();
		}

		@Override
		protected String doInBackground(Void... params) {
			// TODO Auto-generated method stub
				try {
					melist = SpaceService.getMyEvent(page + "", "");
				} catch (MyHttpException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}		
			return null;
		}
		
		@Override
		protected void onPostExecute(String result) {
			super.onPostExecute(result);
			isLoading = false;
			if (flag == 0) {
				hideProgressDialog(); 
			}
			if (isRefresh) {	
				listView.onRefreshComplete();
			} 
			if (melist != null && melist.size() > 0) {
				if (flag == 0) {
					meAdapter = new MyEventListAdapter(context, melist);
					listView.addFooterView(footerView.getFooter());
					listView.setAdapter(meAdapter);
			    	listView.setOnScrollListener(new ScrollListener());
			    	eList = melist;
					
				} else if (isRefresh) {
					isRefresh = false;
					meAdapter = new MyEventListAdapter(context, melist);				
					listView.setAdapter(meAdapter);
					listView.setOnScrollListener(new ScrollListener());
					eList = melist;
					listView.onRefreshComplete();
				} else {
					eList.addAll(melist);
					msAdapter.notifyDataSetChanged();
				}
				if (melist.size() < 5) {
					footerView.showNoData();
				}
				
			} else {
				if (melist != null) {
					if (flag == 0) {
						ll_listView.setVisibility(View.GONE);
						ll_noMsg.setVisibility(View.VISIBLE);
						tips.setText("暂时还没活动");
					} else {
						footerView.showNoData();
					}			
				} else {
					footerView.hideAll();
					showMessage("网络链接失败，请稍后重试！");
				}
				
			}
			flag = 1;		
		}
		
	}
	
	 private class ScrollListener implements OnScrollListener
		{
			private int listViewLastItem = 0;
			private boolean isOver = false;
			@Override
			public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount)
			{
				if (isFromService) {
					if (msAdapter.getCount() % page > 0 || msAdapter.getCount() == 0)
					{
						footerView.hideAll();
						isOver = true;
					}
				} else if (isFromEvent) {
					if (meAdapter.getCount() % page > 0 || meAdapter.getCount() == 0)
					{
						footerView.hideAll();
						isOver = true;
					}
				} else if (isFromCoupon) {
					if (mcAdapter.getCount() % page > 0 || mcAdapter.getCount() == 0)
					{
						footerView.hideAll();
						isOver = true;
					}
				}
				
				listViewLastItem = firstVisibleItem + visibleItemCount - 1; 
			}

			@Override
			public void onScrollStateChanged(AbsListView view, int scrollState)
			{
				if (isFromService) {
					if (!isOver && !isLoading && listViewLastItem >= msAdapter.getCount()-5 
						&& scrollState == OnScrollListener.SCROLL_STATE_IDLE)
					{
						footerView.showLoading();
						sTask = new MyServiceListTask().execute();
					}
				} else if (isFromEvent) {
					if (!isOver && !isLoading && listViewLastItem >= meAdapter.getCount()-5 
							&& scrollState == OnScrollListener.SCROLL_STATE_IDLE)
					{
						footerView.showLoading();
						eTask = new MyEventListTask().execute();
					}
				} else if (isFromCoupon) {
					if (!isOver && !isLoading && listViewLastItem >= mcAdapter.getCount()-5 
							&& scrollState == OnScrollListener.SCROLL_STATE_IDLE)
					{
						footerView.showLoading();
						cTask = new MyCouponListTask().execute();
					}
				}
				

			}
		}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if (v == backButton) {
			finish();
		}
	}


}
